﻿@page "/tabpage"
@inherits BasePage

<TabPage PageName="测试页面" Query="Query" OnChange="OnTabChange">
    <TabContent>
        <TabPane Key="Key1" Tab="测试1">
            <DataTable Model="table1" />
        </TabPane>
        <TabPane Key="Key2" Tab="测试2">
            <DataTable Model="table2" />
        </TabPane>
    </TabContent>
    <TabRight>
        <Toolbar Model="table1.Toolbar" />
    </TabRight>
</TabPage>

@code {
    private string current = "Key1";
    private TableModel<TabDataInfo> table1;
    private TableModel<TabDataInfo> table2;
    private TableModel Query => current == "Key1" ? table1 : table2;

    protected override async Task OnPageInitAsync()
    {
        await base.OnPageInitAsync();

        table1 = new TableModel<TabDataInfo>(this);
        table1.ShowPager = true;
        table1.AddColumn(c => c.Text, true);
        table1.AddColumn(c => c.Number);
        table1.AddColumn(c => c.Decimal);
        table1.AddColumn(c => c.Note);
        table1.Toolbar.AddAction(nameof(New));
        table1.Toolbar.AddAction(nameof(DeleteM));

        table2 = new TableModel<TabDataInfo>(this);
        table2.ShowPager = true;
        table2.AddColumn(c => c.Text, true);
        table2.AddColumn(c => c.Number);
        table2.AddColumn(c => c.Decimal);
        table2.AddColumn(c => c.Date, true);
        table2.AddColumn(c => c.Note);
    }

    public void New() { }
    public void DeleteM() { }

    private async Task OnTabChange(string tab)
    {
        current = tab;
        await StateChangedAsync();

        if (table1.Toolbar.HasItem)
        {
            foreach (var item in table1.Toolbar.Items)
            {
                if (item.Id == nameof(DeleteM))
                    item.Visible = current == "Key1";
            }
            table1.Toolbar.Refresh();
        }
    }

    public class TabDataInfo
    {
        public string Text { get; set; }
        public int? Number { get; set; }
        public decimal? Decimal { get; set; }
        public DateTime? Date { get; set; } = DateTime.Now;
        public string Note { get; set; }
    }
}